/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.j4sc.functionaltests;

import static org.junit.Assert.*;

import java.io.File;
import java.util.Date;

import org.junit.Test;

import com.googlecode.j4sc.PartyInformation;
import com.googlecode.j4sc.PartyInformationBuilder;
import com.googlecode.j4sc.UserCredentials;
import com.googlecode.j4sc.conversion.Conversion;
import com.googlecode.j4sc.conversion.Conversion.Status;
import com.googlecode.j4sc.service.Service;
import com.googlecode.j4sc.submission.Submission;
import com.googlecode.j4sc.submission.SubmissionReference;
import com.googlecode.j4sc.submission.audio.Audio;

public class SubmitAudioAndRetrieveConversion {

    @Test
    public void shouldSubmitAudioAndRetrieveConversionFromTheDevService() throws Exception {
        String reference = String.valueOf(new Date().getTime());
        String accountId = "2009-0599-3047-3905";
        String applicationName = "j4sc";
        Audio audio = new Audio(new File("src/test/resources/alaw-20seconds.wav"));
        String password = getPasswordFromSystemProperties();

        UserCredentials userCredentials = new UserCredentials("javaforspinvoxcreate", password);
        Submission submission = new Submission(accountId, applicationName, reference, audio, userCredentials);

        SubmissionReference submissionReference = Service.DEVELOPMENT.submit(submission);

        Conversion conversion;
        do {
            Thread.sleep(10000);
            conversion = Service.DEVELOPMENT.retrieve(submissionReference);
        } while (conversion.getStatus() == Status.PENDING);

        System.out.println("Status: " + conversion.getStatus());
        System.out.println("Text: " + conversion.getText());
    }

    @Test
    public void shouldSubmitAudioWithPartyInformationAndRetrieveConversionFromTheDevService() throws Exception {
        String reference = String.valueOf(new Date().getTime());
        String accountId = "2009-0599-3047-3905";
        String applicationName = "j4sc";
        Audio audio = new Audio(new File("src/test/resources/alaw-20seconds.wav"));
        String password = getPasswordFromSystemProperties();
        PartyInformation information = new PartyInformationBuilder().withCalledParty("john doe", "12345")
                .withCallingParty("jane doe", "54321").createPartyInformation();

        UserCredentials userCredentials = new UserCredentials("javaforspinvoxcreate", password);
        Submission submission = new Submission(accountId, applicationName, reference, audio, information,
                userCredentials);

        SubmissionReference submissionReference = Service.DEVELOPMENT.submit(submission);

        Conversion conversion;
        do {
            Thread.sleep(10000);
            conversion = Service.DEVELOPMENT.retrieve(submissionReference);
        } while (conversion.getStatus() == Status.PENDING);

        System.out.println("Status: " + conversion.getStatus());
        System.out.println("Text: " + conversion.getText());
    }

    private String getPasswordFromSystemProperties() {
        String password = System.getProperty("password");
        assertNotNull("In order to run this test, you need to set the "
                + "system property 'password' to your account password", password);

        if (password.equals("UNDEFINED")) {
            fail("Run ant with argument -Dpassword=<your password> in order to run this test");
        }
        return password;
    }
}
